package cn.hwadee.muying.controller;

import cn.hwadee.muying.commons.base.BaseController;
import cn.hwadee.muying.commons.jqgrid.GridResult;
import cn.hwadee.muying.commons.page.MyPage;
import cn.hwadee.muying.dto.Client;
import cn.hwadee.muying.entity.User;
import cn.hwadee.muying.service.ClientService;
import cn.hwadee.muying.service.RoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * @创建人 skiner
 * @创建时间 2018/8/7
 * @描述
 */
@Controller
@RequestMapping("/sys/client")
public class ClientController extends BaseController {
    @Autowired
    private ClientService clientService;
    @Autowired
    private RoleService roleService;

    @ModelAttribute("client")
    public Client get(@RequestParam(required = false)Long id){
        if (id != null && id > 0) {
            return clientService.selectById(id);
        }else{
            return new Client();
        }
    }

    /**
     *
     * @param model
     * @return
     */
    @RequiresPermissions("sys:client:list")
    @RequestMapping(value = "/list",produces = "application/json;characset=utf-8")
    public String list(Model model){
        return "/biz/client/list";
    }

    /**
     * 处理客户查询请求
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequiresPermissions("sys:client:list")
    @RequestMapping(value = "/list.json", produces = "application/json;charset=utf-8")
    public GridResult list(Boolean old,String keyword) throws Exception {
        MyPage<Client> page = getPage();
        page = clientService.list(page,getCurrentUser(),old,keyword);
        GridResult result = new GridResult(page.getCurrent(), page.getPages(), page.getCurrent(), page.getRecords());
        return result;
    }

    /**
     * 跳转添加/编辑客户界面
     * @param client
     * @param model
     * @return
     */
    @RequiresPermissions(value = {"sys:client:edit", "sys:client:add"}, logical = Logical.OR)
    @RequestMapping(value = {"/edit", "/add"}, produces = "test/html;charset=utf-8", method = RequestMethod.GET)
    public String add(@ModelAttribute Client client,Model model) throws Exception {
        log.info(client+"");
        model.addAttribute("client", client);
        model.addAttribute("roles", roleService.getCommonRoles());
        return "/biz/client/edit";
    }

    /**
     * 处理更新客户的请求
     * @param client
     * @return
     */
    @RequiresPermissions(value = {"sys:client:edit", "sys:client:add"}, logical = Logical.OR)
    @RequestMapping(value = "/edit", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> edit(@ModelAttribute Client client) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (client.getId() != null && client.getId() > 0) {
                clientService.updateById(client);
            } else {
                User cUser = getCurrentUser();
                clientService.insert(client, cUser);
            }
            result.put("status", 200);
            result.put("msg", "客户信息保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 500);
            result.put("msg", "职员信息保存失败");
        }
        return result;
    }

    /**
     * 处理删除客户的请求
     * @param id
     * @return
     */
    @RequiresPermissions("sys:client:delete")
    @RequestMapping(value = "delete", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (id != null && id > 0) {
                clientService.deleteById(id);
                result.put("status", 200);
                result.put("msg", "删除客户成功");
            } else {
                result.put("status", 500);
                result.put("msg", "请选择删除的客户");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 500);
            result.put("msg", "删除职员失败");
        }
        return result;
    }



}
